# -*- coding: utf-8 -*-
from django import template
# from django.template import Library  # 这样会报错
from django.urls import reverse

from web import models

register = template.Library()


@register.inclusion_tag('inclusion/all_project_list.html')
def all_project_list(request):
    # 1. 获取创建的项目
    my_project_list = models.Project.objects.filter(creator=request.tracer.user)
    # 2. 获取参与的项目
    join_project_list = models.Participant.objects.filter(user=request.tracer.user)
    # 把数据返回到tag:all_project_list.html
    return {'my': my_project_list, 'join': join_project_list, 'request': request}


@register.inclusion_tag('inclusion/manage_menu_list.html')
def manage_menu_list(request):
    data_list = [
        {'title': '概述', 'url': reverse('web:dashboard', kwargs={'project_id': request.tracer.project.id})},
        {'title': '问题', 'url': reverse('web:issues', kwargs={'project_id': request.tracer.project.id})},
        {'title': '统计', 'url': reverse('web:statistics', kwargs={'project_id': request.tracer.project.id})},
        {'title': 'wiki', 'url': reverse('web:wiki', kwargs={'project_id': request.tracer.project.id})},
        {'title': '文件', 'url': reverse('web:file', kwargs={'project_id': request.tracer.project.id})},
        {'title': '配置', 'url': reverse('web:setting', kwargs={'project_id': request.tracer.project.id})},
    ]
    
    # 当点击时，默认选中
    for item in data_list:
        # 当前访问的url
        if request.path_info.startswith(item['url']):
            item['class'] = 'active'
    
    return {'data_list': data_list}
